package com.wz.project.system.item.controlle;

import com.wz.common.utils.security.ShiroUtils;
import com.wz.framework.web.controller.BaseController;
import com.wz.project.system.employee.domain.Employee;
import com.wz.project.system.employee.domain.EmployeeGroup;
import com.wz.project.system.employee.service.EmployeeService;
import com.wz.project.system.item.domain.ItemDetails;
import com.wz.project.system.item.service.ItemService;
import com.wz.project.system.user.domain.User;
import com.wz.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/system/itemQuery")
public class ItemQueryController extends BaseController {
    private String prefix = "system/item";

    @Autowired
    private ItemService itemService;

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private IUserService userService;

    @GetMapping()
    public String Item() {
        return prefix + "/itemQuery";
    }

    /**
     * 页面浏览
     * @param itemDetails
     * @return
     */
    @PostMapping("list")
    @ResponseBody
    public Object list(ItemDetails itemDetails) {
        //获取当前登陆对象
        /*String loginName = ShiroUtils.getLoginName();*/
        //手机号

        //获取当前用户的Id
        Long userId = ShiroUtils.getUserId();
        User user = userService.selectUserById(userId);
        //获取到用户的手机号（也是员工的手机号）
        String phonenumber = user.getPhonenumber();

        //通过员工的手机号获取到员工的id
        Employee employee=employeeService.selectEmployeeByPhonenumber(phonenumber);

        if (user.isAdmin()){
            startPage();
            List<ItemDetails> list=itemService.selectItemDetailsList(itemDetails);
            return getDataTable(list);
        }else {
            //员工获取Id
            Integer employeeId = employee.getEmployeeId();

            //获取当前用户全部用户组的Id
            List<EmployeeGroup> employeeGroupLsit=employeeService.selectEmployeeGroupIdBy(employeeId);
            if(employeeGroupLsit.size()>0){
                StringBuilder sb = new StringBuilder();
                int count=0;
                for (EmployeeGroup groupIDs:employeeGroupLsit) {
                    sb.append(groupIDs.getPrjgroupId());
                    count++;
                    if (count<employeeGroupLsit.size()){
                        sb.append(",");
                    }
                }

                itemDetails.setPrjgroupIds(sb.toString());
                startPage();
                List<ItemDetails> list=itemService.selectItemDetailsList(itemDetails);
                return getDataTable(list);
            }else{
                return getDataTable(new ArrayList<>());
            }
        }

    }
}